package com.jinyi.wwdeng.ui.main.impl

import android.content.Context
import com.jinyi.base.mvp.BasePresenter
import com.jinyi.wwdeng.AppConfigs
import com.jinyi.wwdeng.ui.main.contract.CreditAuditContract
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/28 16:21
 * @Annotation：Presenter 实现类
 */
class CreditAuditPresenterImpl : BasePresenter<CreditAuditContract.View, CreditAuditModelImpl>(),
    CreditAuditContract.Presenter {

    override fun createModel(): CreditAuditModelImpl {
        return CreditAuditModelImpl(this)
    }

    /**
     * 获取用户贷款额度预览信息
     */
    fun loadCreditLimitInfoRequest() {
        if (isViewDestory()) {
            return
        }
        model!!.loadCreditLimitInfoRequest()
        model!!.loadRecommendAppListRequest()
    }

    /**
     * 获取已绑定的账户列表（法语）
     */
    fun loadBindAccountListAction() {
        if (isViewDestory()) {
            return
        }
        model!!.loadBindAccountListAction()
    }

    /**
     * 获取评分配置信息
     */
    fun loadScoreCommentAction() {
        model!!.loadScoreCommentRequest()
    }

    /**
     * 添加绑定账户
     */
    fun addBindAccountAction(mobile: String, walletChannel: String, bindFlag:String="NO") {
        if (isViewDestory()) {
            return
        }
        mvpView!!.showLoading("")
        model!!.addBindAccountAction(mobile, walletChannel, bindFlag)
    }

    /**
     * 删除账户
     */
    fun deleteAccountAction(info: JSONObject) {
        if (isViewDestory()) {
            return
        }
        mvpView!!.showLoading("")
        model!!.deleteAccountAction(info)
    }

    /**
     * 获取贷款额度信息成功
     */
    override fun onCreditLimitInfoSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when (data.optInt("code")) {
            0 -> {//成功
                val obj = data.optJSONObject("obj")
                mvpView!!.showCreditLimitInfoData(obj)
            }

            else -> {//失败
                mvpView!!.showToast(data.optString("msg"))
            }
        }
    }

    /**
     * 获取推荐安装的app列表
     */
    override fun onRecommendAppListSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when (data.optInt("code")) {
            0 -> {//成功
                val obj = data.optJSONObject("obj")
                val appList = arrayListOf<JSONObject>()
                if (obj.has("dcProductList")) {
                    val appArray = obj.optJSONArray("dcProductList")
                    if (appArray != null && appArray.length() > 0) {
                        for (i in 0..<appArray.length()) {
                            appList.add(appArray.optJSONObject(i))
                        }
                    }
                }
                mvpView!!.showRecommendAppList(appList)
            }

            else -> {//失败
                mvpView!!.showToast(data.optString("msg"))
            }
        }
    }

    /**
     * 评分配置获取成功
     */
    override fun onScoreCommentSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when (data.optInt("code")) {
            0 -> {//成功
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    mvpView!!.showScoreCommentData(obj)
                }
            }

            else -> {//失败
                mvpView!!.showToast(data.optString("msg"))
            }
        }
    }

    /**
     * 获取已绑定的账户列表成功
     */
    override fun onBindAccountListSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when (data.optInt("code")) {
            0 -> {//成功
                val accountList = arrayListOf<JSONObject>()
                val array = data.optJSONArray("obj")

                if (array != null && array.length() > 0) {
                    for (i in 0..<array.length()) {
                        val item = array.optJSONObject(i)
                        item.put("isEdit", false)
                        item.put("isShowDelete", false)
                        accountList.add(item)
                    }
                }

                if (accountList.size < 2) {
                    for (i in 0..<2 - accountList.size) {
                        val item = JSONObject()
                        item.put("isEdit", true)
                        accountList.add(item)
                    }
                }
                mvpView!!.setBindAccountList(accountList)
            }

            else -> {//失败
                mvpView!!.showToast(data.optString("msg"))
            }
        }
    }

    /**
     * 添加绑定账户成功
     */
    override fun addBindAccountSuccess(mobile: String, data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when (data.optInt("code")) {
            0 -> {//成功
                val obj = data.optJSONObject("obj") ?: JSONObject()

                // "YES/NO 是否需要提示用户这个号码可以绑另一个渠道的钱包"
                val addBindFlag = obj.optString("addBindFlag")
                if (addBindFlag == "YES") {
                    //需要提示绑定另一个渠道账户
                    var otherAccountInfo: JSONObject? = null

                    //用于从APP配置中获取（walletChannels）对应的信息
                    val channel = obj.optString("channel")
                    val commonData =
                        JSONObject(AppConfigs.getSPString(AppConfigs.SP_KEY.SP_HENECREDIT_COMMON))
                    val customize = commonData.optJSONObject("customize")
                    if (customize != null) {
                        val walletChannels = customize.optJSONArray("walletChannels")
                        if (walletChannels != null && walletChannels.length() > 0) {
                            for (i in 0..<walletChannels.length()) {
                                val item = walletChannels.optJSONObject(i)
                                if (channel == item.optString("value")) {
                                    otherAccountInfo = item
                                    break
                                }
                            }
                        }

                        // 只有非空的情况下才处理
                        if (otherAccountInfo != null) {
                            otherAccountInfo.put("walletMobile", mobile)
                            //提示绑定另一个渠道账户
                            mvpView!!.showBingAccountOtherDialog(otherAccountInfo)
                            return
                        }
                    }
                }

                //刷新绑定的账户列表
                mvpView!!.showLoading("")
                model!!.loadBindAccountListAction()
            }

            else -> {//失败
                mvpView!!.showToast(data.optString("msg"))
            }
        }
    }

    /**
     * 删除已绑定的账户成功
     */
    override fun onDeleteAccountSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        mvpView!!.showToast(data.optString("msg"))
        //获取已绑定的账户列表
        loadBindAccountListAction()
    }

    override fun onRequestFinish() {
        mvpView!!.onRequestFinish()
    }

    override val context: Context?
        get() = if (!isViewDestory()) {
            mvpView!!.getContext()
        } else null
}